package com.rr.easy;

import com.rr.entity.TreeNode;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @author RR
 * @date 2023/1/9 21:54
 */
/*
* 层级遍历树
* */
public class BfsScanTree {

    public static void bfcScanTree(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        while (queue.size() > 0) {
            TreeNode remove = queue.poll();
            System.out.println(remove.val);
            if (remove.left != null) {
                queue.add(remove.left);
            }
            if (remove.right != null) {
                queue.add(remove.right);
            }
        }
    }

    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        TreeNode t4 = new TreeNode(4);
        TreeNode t5 = new TreeNode(5);
        TreeNode t6 = new TreeNode(6);
        t1.left = t2;
        t1.right = t3;
        t2.right = t4;
        t3.left = t5;
        t3.right = t6;
        bfcScanTree(t1);
    }
}
